"""
ideally, we can just use treesitter, so why bother write our own ast_tree?
Yes, why? Tell me attribute, tell me why
"""

from typing import List

from tree_sitter import Node


class RustNode:
    def __init__(self):
        pass


class AttributedItemNode(RustNode):
    def __init__(self, attributes, item):
        self._attributes = attributes
        self._item = item
        texts = [a.text for a in attributes]
        texts.append(item.text)
        self.text = b"\n".join(texts)
        # self.type = item.type
        # well, one must keep in mind that this is not a real item
        # self.children = None

    def __getattr__(self, attr):
        if attr in self.__dict__:
            return getattr(self, attr)
        return getattr(self._item, attr)


def parse_on_treesit_nodes(rust_nodes: List[Node]) -> List[Node]:
    package = []
    res = []
    for n in rust_nodes:
        if n.type == "attribute_item":
            package.append(n)
        elif (not len(package) == 0) and not (n.type == "block_comment" or n.type == "line_comment"):
            res.append(AttributedItemNode(package, n))
            package = []
        else:
            res.append(n)
    if not len(package) == 0:
        res = res + package
    return res
